<template>
    <div class="mod-active-code">
        <van-nav-bar class="nav-bar"
                     title="我的邀请码"
                     left-text="返回"
                     left-arrow
                     @click-left="$router.go(-1)"
        />
        <div>
            <div class="header">
                <div class="header-l">
                    <span class="num">{{activeCodeCt}}</span>
                    <div class="desc">可领取邀请码数量</div>
                </div>
                <div class="header-r" v-if="activeCodeCt!==0">
                    <van-button color="#ff5a60" plain size="small" @click="getac" class="btn-submit" round  >领取</van-button>
                </div>
                <div class="header-r" >
                    <van-button color="#ff5a60" plain size="small" @click="$router.push('/awardCode')" class="btn-submit" round  >发放</van-button>
                </div>
            </div>
            <div style="padding: 20px;">可用数量：{{activeCodeList.length}}</div>
            <van-list
                    v-model="loading"
                    :finished="finished"
                    finished-text="没有更多了"
                    @load="onLoad"
            >
                <div class="item" v-for="item in activeCodeList" :key="item.id" >
                    <div class="item-a">
                        <div>邀请码：{{item.activationCode}}</div>
                        <div>状态：
                            <span v-if="item.status === -1">已失效</span>
                            <span v-if="item.status === 1">已使用</span>
                            <span v-else>可用</span>
                        </div>
                    </div>
                    <div class="item-a">
                        <div>获取时间：{{item.createTime | moment("MM-DD HH:mm")}}</div>
                        <div><van-button round @click="doCopy(item.activationCode)" v-if="item.status === 0" style="width:80px;height:25px;font-size:12px;" color="linear-gradient(to right, #4bb0ff, #6149f6)">分享好友</van-button></div>
                    </div>
                </div>
            </van-list>
        </div>
        <div class="prompt_box" v-show="prompt_box_show">
            <div class="prompt_box-title">领取邀请码</div>
            <div class="input-box">
                <span>领取数量</span>
                <input type="number" v-model="num" placeholder="请输入领取数量" />
            </div>
            <div class="btns">
                <button type="button" @click="confirmprompt" class="confirm">确定</button>
                <button type="button" @click="cancelprompt" class="cancel">取消</button>
            </div>
        </div>
        <div class="mask" v-show="prompt_box_show"></div>
    </div>
</template>

<script>
    import api from '@/api';
    import {mapActions, mapState} from 'vuex'
    // import api from '@/api'
    // import {clearLoginInfo} from '@/utils'
    export default {
        name: 'User',
        inject: ['reload'],
        data (){
            return{
                loading: false,
                finished: false,
                prompt_box_show: false,
                num: 1
            };
        },
        created(){
            this.getActiveCodeList()
        },
        computed: {
            ...mapState('user', ['activeCodeList','activeCodeCt'])
    },
    methods: {
    ...mapActions('user',['getActiveCodeList']),
            onLoad() {
            this.loading = true
            this.getActiveCodeList()
            this.finished = true
            this.loading = false
        },

        getac () {
            // this.prompt_box_show = true
            let that = this
            api.getActiveCode({
                'num': parseInt(this.num)
            }).then(res => {
                console.log(res)
            if (res.data.code === 0) {
                this.$toast.success('领取成功');
                setTimeout(function () {
                    that.reload()
                }, 1000)

            } else {
                this.$toast.fail('领取失败');
            }
        });
        },
        confirmprompt: function () {
            let that = this
            api.getActiveCode({
                'num': this.num
            }).then(res => {
                console.log(res)
            if (res.data.code === 0) {
                this.$toast.success('领取成功');
                setTimeout(function () {
                    that.reload()
                }, 1000)

            } else {
                this.$toast.fail('领取失败');
            }
        });

            // this.prompt_box_show  = false
        },
        cancelprompt: function () {
            this.prompt_box_show  = false
        },
        doCopy: function (code) {
            this.$router.push( {name:'inviter',  query:{code: code}})

            // let url = window.location.href;
            // url=url.replace(this.$route.path,"");
            // console.log(url)
            // let that = this
            // this.$copyText(url+'/register?code='+code).then(function () {
            //     that.$toast('分享链接，复制成功')
            // }, function () {
            //     that.$toast('复制失败')
            // })
        }
    }
    }
</script>

<style lang="scss" scoped>
    .mod-active-code {

        .header{
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 300px;
            background: linear-gradient(#ff5a60, #ff827b);
            .header-l{
                color: #373737;
                height: 200px;
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                flex: 1;
                .num{
                    font-size: 80px;
                    color: #fff;
                }

                .desc{
                    font-size:30px;
                    color:#fff;
                }
            }
            .header-r{
                width: 200px;
                .btn-submit{
                    width: 120px;
                }
            }
        }

        .item{
            border-top:1px solid #f2f2f2;
            padding:20px;
            .item-a{
                line-height: 60px;
                color: #646464;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
        }

        .prompt_box {
            position: fixed;
            width: 90%;
            height: 380px;
            top: 50%;
            left: 5%;
            margin-top: -150px;
            z-index: 100;
            background: #FFFFFF;
            border-radius: 10px;
            overflow: hidden;

            .prompt_box-title {
                text-align: center;
                height: 74px;
                line-height: 74px;
                border-bottom: 1px solid #EEEEEE;
            }
            .input-box {
                height: 230px;
                display: flex;
                align-items: center;
                padding: 0 20px;
                span {
                    color: #999999;
                    font-size: 24px;
                    margin-right: 10px;
                }
                input {
                    flex: 1;
                    border: 1px solid #EEEEEE;
                    height: 75px;
                    font-size: 24px;
                    padding: 0 10px;
                }
            }
            .btns {
                height: 75px;
                overflow: hidden;
                button {
                    width: 50%;
                    height: 75px;
                    line-height: 75px;
                    float: left;
                    background: #EEEEEE;
                    border: none;
                }
                .confirm {
                    background: #AC2925;
                    color: #FFFFFF;
                }
            }
        }
        .mask {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.4);
            z-index: 99;
        }
    }
</style>
